package com.zhiche.lisa.bms.controller.driver;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.pojo.vo.driver.PriceconfFreightVO;
import com.zhiche.lisa.bms.pojo.vo.driver.PriceconfOcVO;
import com.zhiche.lisa.bms.service.driver.IPriceconfOcService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 车型计价油耗 前端控制器
 * </p>
 *
 * @author zhaoguixin
 * @since 2018-08-30
 */
@RestController
@RequestMapping("/vehicle_oc")
@Slf4j
@Api(description = "车型计价油耗", tags = {"车型计价油耗"})
public class PriceconfOcController {

    @Autowired
    private IPriceconfOcService priceconfOcService;

    @PostMapping(value = "/page_list")
    @ApiOperation(value = "分页获得车型计价油耗", notes = "分页获得车型计价油耗",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceconfOcVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PriceconfOcVO>> listByPage(@ApiParam(name = "PriceconfOcVO", value = "线路计价里程VO", required = true) @RequestBody Page<PriceconfOcVO> pageVo) {
        log.info("PriceconfOcController.listByPage params : {}", pageVo);
        RestfulResponse<Page<PriceconfOcVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(priceconfOcService.listByPage(pageVo));
        } catch (BaseException e) {
            log.error("PriceconfOcController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfOcController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @GetMapping(value = "/getById")
    @ApiOperation(value = "根据ID得到车型计价油耗", notes = "根据ID得到车型计价油耗",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "id", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceconfFreightVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PriceconfOcVO> getById(Integer id) {
        log.info("PriceconfOcController.getById params : {}", id);
        RestfulResponse<PriceconfOcVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(priceconfOcService.getById(id));
        } catch (BaseException e) {
            log.error("PriceconfOcController.getById ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfOcController.getById ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/add")
    @ApiOperation(value = "添加", notes = "添加",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Integer.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Integer> add(@ApiParam(name = "PriceconfOcVO", value = "车型计价油耗VO", required = true) @RequestBody @Valid PriceconfOcVO form, BindingResult br) {
        log.info("PriceconfOcController.add params : {}", form);
        RestfulResponse<Integer> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(priceconfOcService.add(form, br));
        } catch (BaseException e) {
            log.error("PriceconfOcController.add ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfOcController.add ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/update")
    @ApiOperation(value = "修改", notes = "修改",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Integer.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> update(@ApiParam(name = "PriceconfOcVO", value = "车型计价油耗VO", required = true) @RequestBody @Valid PriceconfOcVO form, BindingResult br) throws Exception {
        log.info("PriceconfOcController.update param : {}", form);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = priceconfOcService.update(form, br);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("PriceconfOcController.update ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfOcController.update ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/del")
    @ApiOperation(value = "删除", notes = "删除",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "Integer", name = "id", value = "id", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> del(Integer id) {
        log.info("PriceconfOcController.del param : {}", id);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = priceconfOcService.del(id);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("PriceconfOcController.del ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfOcController.del ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}

